import _ from 'lodash';
export const isNumber = (value: string) => {
    let reg = /^[0-9]+.?[0-9]*$/;
    return reg.test(value);
}

export function camelCase(name: string) {
    return name.replace(/-(\w)/g, (_: string, n: string) => {
        return n.toUpperCase();
    });

}

export function guid(len: number = 32, radix: number = 16) {
    const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
    const uuid: string[] = [];
    radix = radix || chars.length;

    if (len) {
        // Compact form
        for (let i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
    } else {
        // rfc4122, version 4 form

        // rfc4122 requires these characters
        uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
        uuid[14] = '4';

        // Fill in random data. At i==19 set the high bits of clock sequence as
        // per rfc4122, sec. 4.1.5
        for (let i = 0; i < 36; i++) {
            if (!uuid[i]) {
                const r = 0 | Math.random() * 16;
                uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
            }
        }
    }

    return uuid.join('');
}

export function camelCaseObject(obj: object): object {
    let rt = {} as object;
    if (obj && obj.constructor == String) {
        obj['split'](';').forEach(item => {
            const is = item.split(':');
            _.set(rt, is[0] || '', is[1] || '');
        });
    } else {
        for (let key in obj) {
            _.set(rt, camelCase(key), _.get(obj, key));
        }
    }
    return rt;
}

export function getRowIndex(id: string): number {
    return parseInt(id.replace('row_', ''), 0);
}

export function getColIndex(id: string): number {
    return parseInt(id.replace('col_', ''), 0);
}

export function getDomId(fieldid: string, detail?: string, index?: string | number) {
    if (detail) {
        return `${fieldid}_${index}`;
    } else {
        return fieldid;
    }
}
export function getDomIdByFieldKey(fieldPath: string) {
    const fieldpaths = fieldPath.split('.');
    if (fieldpaths.length == 3) {
        return `${fieldpaths[2]}_${fieldpaths[1]}`;
    } else {
        return fieldPath;
    }
}


export function checkHref(href: string) {
    let exceptArr = [
        "/spa/workflow/index_form.jsp",
        "/spa/document/index.jsp",
        "/spa/hrm/index_mobx.html",
        "/spa/cube/index.html",
        "/spa/cube/index.jsp",
    ];
    for (let index = 0; index < exceptArr.length; index++) {
        const element = exceptArr[index];
        if (href.indexOf(element) > -1) {
            href = href.replace(/(jsp#)|(html#)/g, item => {
                return item.replace(/(jsp)|(html)/g, type => {
                    return type + `?__random__=${Date.now()}`;
                });
            });
            return href;
        }
    }
    return href;
}

export function getUrlParams() {
    if (!window.location.href.includes('?')) return;
    const hrefArr = window.location.href.split('?');
    const paramsArr = hrefArr.length > 1 ? hrefArr[hrefArr.length - 1].split('&') : [];
    let urlParams :any = {};
    paramsArr.forEach(item => {
        const key_val = item.split('=');
        urlParams[key_val[0]] = key_val[1];
    });
    return urlParams;
};
